import scipy.misc
import os
import sys
from paralib import Paralib
from dip_celery import convert_img_format
import coloredlogs
import logging

directory = 'static/diag_result'
# flist = os.listdir(directory)
# for index,file in enumerate(flist):
#     if file.find('or') < 0:
#         continue
#     if file.find('L') <0 and file.find('R')<0:
#         continue
#     print(file,':',index,'/',len(flist), end='\r')
#     img = scipy.misc.imread(directory+'/'+file)
#     file = file[:-4]+'.jpg'
#     scipy.misc.imsave(directory+'/'+ file,img)

class Converter(Paralib):
    def __init__(self):
        super().__init__()
        self.flist = os.listdir(directory)
        self.current = 0

    def run(self):
        while self.current < len(self.flist):
            fname = self.flist[self.current]
            print(self.current,'/',len(self.flist),end='\r')
            self.current += 1
            if not fname.endswith('.png'):
                continue
            return convert_img_format.delay(directory+'/'+fname)
        return None

if __name__ == '__main__':
    coloredlogs.install(level='DEBUG', logger=logging.getLogger(),
                        fmt='%(filename)s:%(lineno)d %(message)s',
                        datefmt='%H:%M:%S',
                        )
    convert = Converter()
    convert.start()
